package com.wlstock.funi.ui;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.em.baselistener.OnBackListener;
import com.em.baselistener.OnMoreListener;
import com.em.baselistener.OnTitleClickListener;
import com.em.widget.view.SelectPopUpWindow;
import com.em.widget.view.SelectPopUpWindow.onPopViewListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.wlstock.funi.R;
import com.wlstock.funi.adapter.TradeAdapter;
import com.wlstock.funi.app.FundApplication;
import com.wlstock.funi.constant.ServiceConstants;
import com.wlstock.funi.constant.URLConstant;
import com.wlstock.funi.dao.SelfFundDao;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.model.SelfFundModel;
import com.wlstock.funi.request.CustomStringRequest;
import com.wlstock.funi.request.EventBusConstants;
import com.wlstock.funi.request.NetWorkListener;
import com.wlstock.funi.service.LatestPriceService;
import com.wlstock.funi.service.MissionService;
import com.wlstock.funi.service.QueryHisService;
import com.wlstock.funi.utils.IntentContants;
import com.wlstock.funi.utils.ServiceUtil;
import com.wlstock.funi.utils.StockUtils;
import com.wlstock.funi.utils.TimeUtils;
import com.wlstock.funi.utils.ToastUtils;

public class StartActivity extends FundBaseActivity implements OnClickListener, OnBackListener, OnMoreListener, NetWorkListener, onPopViewListener, OnTitleClickListener{
	private static final String zf5 = "zf5";
	
	private final String TRADE_TRACK = "2";//交易跟踪
	private final String OPERAT_LIST = "1";//操作列表
	public Activity activity;
	private Button btn_stop;
	private Button btn_start;
	private ListView listview_trade;
	private TradeAdapter tradeAdapter;
	private ArrayList<OperateModel> operateModels;
	private SelectPopUpWindow selectPopUpWindow;
   
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		initView();
		
	}
	
	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		super.initView();
		setTitle(getString(R.string.title_traker_list));
		onBackClick(this);
		showMoreBtn(this);
		onTitleClick(this);
		
//		View public_actionbar_more = findViewById(R.id.public_actionbar_more);
//		public_actionbar_more.setVisibility(View.VISIBLE);
//		public_actionbar_more.setOnClickListener(this);
		
		btn_stop = (Button) findViewById(R.id.btn_stop);
		btn_start = (Button) findViewById(R.id.btn_start);
		findViewById(R.id.btn_query_his).setOnClickListener(this);
		findViewById(R.id.btn_to_log).setOnClickListener(this);
		listview_trade = (ListView) findViewById(R.id.listview_trade);
		btn_start.setOnClickListener(this);
		btn_stop.setOnClickListener(this);
		
		findViewById(R.id.btn_to_buy).setOnClickListener(this);
		findViewById(R.id.btn_to_set).setOnClickListener(this);
		
		operateModels = new ArrayList<OperateModel>();
		tradeAdapter = new TradeAdapter(this,operateModels);
		listview_trade.setAdapter(tradeAdapter);
			
		initData();
	}
	
	private void initData() {
		// TODO 初始化数据
		List<SelfFundModel> zbSelfFundModels = StockUtils.getZBSelfFundModels();
		for (int i = 0; i < zbSelfFundModels.size(); i++) {
			SelfFundDao.getInstance(this).addOrUpdateSelfModel(zbSelfFundModels.get(i));
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		HashMap<String, String> parms = new HashMap<String, String>();
		parms.put("act", OPERAT_LIST);
		
		CustomStringRequest curRequest = new CustomStringRequest();
		curRequest.networkResponseListener = this;
		curRequest.requestGet(URLConstant.WLHOST, URLConstant.WL_TRADE_TRACK, null, parms);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
//		EventBus.getDefault().unregister(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_start:
			ServiceUtil.invokeTrackService(this);
			ServiceUtil.invokeService(this, LatestPriceService.class, ServiceConstants.LatestPriceService_ACTION_START,ServiceConstants.ELAPSED_TIME_UPDATE_PPICE);
			ServiceUtil.invokeService(this, MissionService.class, ServiceConstants.MissionService_ACTION_START,ServiceConstants.OneSecond);
			break;
		case R.id.btn_stop:
			ServiceUtil.cancelTrackService(this);
			ServiceUtil.cancelService(this, LatestPriceService.class, ServiceConstants.LatestPriceService_ACTION_START);
			ServiceUtil.cancelService(this, MissionService.class, ServiceConstants.MissionService_ACTION_START);
			FundApplication.requestTimes = 0;
			FundApplication application =(FundApplication)getApplicationContext();
			application.getRequestQueue().stop();
			break;
		default:
			break;
		}
	}

	private List<Map<String, Object>> getMenuList()
	{
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_log));
		map.put("icon", R.drawable.actionbar_camera_icon);
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_set));
		map.put("icon", R.drawable.actionbar_camera_icon);
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_his));
		map.put("icon", R.drawable.actionbar_camera_icon);
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_selfmodeact));
		map.put("icon", R.drawable.actionbar_camera_icon);
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_test));
		map.put("icon", R.drawable.actionbar_camera_icon);
		list.add(map);
		
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_caculate_md));
		map.put("icon", R.drawable.actionbar_camera_icon);
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_op_bs_activity));
		map.put("icon", R.drawable.actionbar_camera_icon);
		list.add(map);
		
//		map = new HashMap<String, Object>();
//		map.put("name", getResources().getString(R.string.pop_title_fund_set_activity));
//		map.put("icon", R.drawable.actionbar_camera_icon);
//		list.add(map);
		
		return list;
	}
	
	@Override
	public void onMoreClick(View v) {
		// TODO Auto-generated method stub
		if (selectPopUpWindow == null) {
			selectPopUpWindow = new SelectPopUpWindow(this, getMenuList());
			selectPopUpWindow.popListener = this;
		}
		selectPopUpWindow.showDropDown(findViewById(R.id.layout_top_but));
	}

	@Override
	public void onBackClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onTitleClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onErrorResponse(VolleyError error, EventBusConstants eventCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onErrorResponse(VolleyError error, String eventCode) {
		// TODO Auto-generated method stub
		if (eventCode.equals(URLConstant.WL_TRADE_TRACK)) {
			
		}
	}

	@Override
	public void onResponse(String response, EventBusConstants eventCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResponse(String response, String eventCode) {
		// TODO 处理返回内容
		if (URLConstant.WL_TRADE_TRACK.equals(eventCode)) {
				try {
				JSONObject obj = new JSONObject(response);
				Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
				String format = TimeUtils.patternDate(obj.getString("datas"));
				operateModels = gson.fromJson(format, new TypeToken< List<OperateModel> >(){}.getType());
				tradeAdapter.setModels(operateModels);
				tradeAdapter.notifyDataSetChanged();
//				List<OperateModel> filterModels = CaculateUtils.filterRepeatModels(operateModels);
//				SelfFundDao.getInstance(this).addWithOperateModeList(operateModels);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if (zf5.equals(eventCode)) {
			ToastUtils.showMiddleMsg(this, response);
		}
	}

	@Override
	public void onViewSelectListener(String str) {
		if (str != null) {
			if (str.equals(getResources().getString(R.string.pop_title_log))) {
				//登录页面
				Intent logIt = new Intent(StartActivity.this, LoginActvity.class);
				startActivity(logIt);
			}else if (str.equals(getResources().getString(R.string.pop_title_set))) {
				//去到设置页面
				Intent setIt = new Intent(StartActivity.this, SettingActivity.class);
				startActivity(setIt);
			}else if (str.equals(getResources().getString(R.string.pop_title_his))) {
				//获取操盘手历史
				Intent i = new Intent(StartActivity.this, QueryHisService.class);
				i.setAction(ServiceConstants.QueryHisService_ACTION_START);
				startService(i);
			}else if (str.equals(getResources().getString(R.string.pop_title_test))) {
				//测试交易
//				Intent selfFundIntent = new Intent(StartActivity.this, PopTradeDialog.class);
//				startActivity(selfFundIntent);
				
				//测试悬浮窗口
//				LinearLayout layout = new LinearLayout(this);
//				layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//				layout.setOrientation(LinearLayout.VERTICAL);
//				TextView tV = new TextView(this);
//				tV.setLayoutParams(new LinearLayout.LayoutParams(200, 200));
//				layout.addView(tV);
//				tV.setText("哼哼哈哈");
//				tV.setTextSize(30);
//				tV.setTextColor(getResources().getColor(R.color.white));
//				FundApplication.getInstance().showDesk(layout);
								
			}else if (str.equals(getResources().getString(R.string.pop_title_caculate_md))) {
				//计算md5
				ServiceUtil.invokeService(this, MissionService.class, ServiceConstants.MissionService_ACTION_START,ServiceConstants.OneSecond);
			}else if(str.equals(getResources().getString(R.string.pop_title_selfmodeact))){
				//去到自选界面
				Intent selfFundIntent = new Intent(StartActivity.this, SelfFundActivity.class);
				startActivity(selfFundIntent);
			}else if (str.equals(getResources().getString(R.string.pop_title_op_bs_activity))) {
				Intent bsActivityIntent = new Intent(StartActivity.this, OpBSMainActivity.class);
				startActivity(bsActivityIntent);
			}else if (str.equals(getResources().getString(R.string.pop_title_fund_set_activity))) {
				//去单独设置页面
				Intent intent = new Intent(StartActivity.this, SelfFundSetActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(IntentContants.INTENT_STKCODE, "000050");
				bundle.putString(IntentContants.INTENT_STKNAME, "深天马a");
				intent.putExtras(bundle);
				startActivity(intent);
			}
		}
	}

}
